package edu.sctu.wangfuxin.day20150628.data;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by user06 on 2015/6/28.
 */

/**
 * 获取Json数据类
 */
public class GetJsonData {
    /**
     *
     * @param urlpath URl
     * @param path 保存网页上数据的文件路径
     * @param param Post请求时的值
     * @return
     */


    public String getJsonData(String urlpath, String path, String param) {

        byte[] bytes = new byte[1024];
        int count;
        PrintWriter out;
        String string = "";

        try {
            URL url = new URL(urlpath);
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                out = new PrintWriter(httpURLConnection.getOutputStream());
                out.print(param);
                InputStream inputStream = httpURLConnection.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(path);
                while ((count = inputStream.read(bytes)) != -1) {
                    fileOutputStream.write(bytes, 0, count);
                    string = string + new String(bytes, 0, count, "utf-8");
                }
                out.close();
                inputStream.close();
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        return string;
    }

}
